<?php

/**
 * 重构分析
 *
 * @author haifeng.wu
 */

require_once 'CommonController.php';
class Admin_RebuyController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->clientInstallerModel = new Application_Model_ClientInstaller();
        $this->orderModel = new Application_Model_Order();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        $vars = array();
        $wheres = array();
        $vars['startYear'] = $this->_request->getPost('startYear');
        $vars['startMonth'] = $this->_request->getPost('startMonth');
        $vars['endYear'] = $this->_request->getPost('endYear');
        $vars['endMonth'] = $this->_request->getPost('endMonth');
        $vars['syear'] = $this->_request->getPost('syear');
        $vars['smonth'] = $this->_request->getPost('smonth');
        $vars['eyear'] = $this->_request->getPost('eyear');
        $vars['emonth'] = $this->_request->getPost('emonth');
        $this->view->vars = $vars;
        $productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $res = array();
        if ($this->_request->isPost()) {
            if (($vars['endYear'].$vars['endMonth'])*1.0 >= ($vars['startYear'].$vars['startMonth'])*1.0 && $vars['startMonth'] != 'X' && $vars['endMonth'] != 'X') {
                $sqlBig = "SELECT count(udid) AS bigudid 
                             FROM (
                                SELECT udid FROM mob_order 
                                WHERE `productCode`='{$productCode}' 
                                AND `addTime` > '" .$vars['startYear'] . "-" . $vars['startMonth']. "-1' 
                                AND `addTime` < '" .$vars['startYear'] . "-" . ($vars['startMonth']+1) . "-1' 
                                GROUP BY udid
                             ) ddd "; //9月至少有一笔提交成功订单的用户数
                $bigRow = $this->orderModel->getAdapter()->fetchAll($sqlBig);
                $sqlSmall = "SELECT count(udid) AS smalludid FROM (
                                SELECT b.udid FROM mob_order AS a INNER JOIN mob_order AS b 
                                ON a.udid = b.udid
                                WHERE `a`.`productCode`='{$productCode}' 
                                AND `b`.`productCode`='{$productCode}' 
                                AND `a`.`addTime` > '" .$vars['endYear'] . "-" . $vars['endMonth']. "-1' 
                                AND `a`.`addTime` < '" .$vars['endYear'] . "-" . ($vars['endMonth']+1) . "-1' 
                                AND `b`.`addTime` > '" .$vars['startYear'] . "-" . $vars['startMonth']. "-1' 
                                AND `b`.`addTime` < '" .$vars['startYear'] . "-" . ($vars['startMonth']+1) . "-1' 
                                GROUP BY a.udid,b.udid  
                            )sss";//9月至少有一笔提交成功订单且在10月至少有一笔提交成功订单的用户数
                $smallRow = $this->orderModel->getAdapter()->fetchAll($sqlSmall);
                $arr = array();
                $arr['smallname'] = $vars['startYear'] . '年' . $vars['startMonth'].'月至少有一笔提交成功订单且在'.$vars['endYear'] . "年" . $vars['endMonth'].'月至少有一笔提交成功订单的用户数';
                $arr['small'] = $smallRow[0]['smalludid'];
                $arr['big'] = $bigRow[0]['bigudid'];
                $arr['bigname'] = $vars['startYear'] . '年' . $vars['startMonth'].'月至少有一笔提交成功订单的用户数';
                $arr['pername'] = $vars['startYear'] . '年' . $vars['startMonth'].'月订单用户'.$vars['endYear'] . "年" . $vars['endMonth'].'月重购占比';
                $arr['per'] = ($arr['big'] == 0) ? '-' : round(($arr['small']/$arr['big'])*100, 2).'%';
                $res[] = $arr;
            }
            if (($vars['eyear'].$vars['emonth'])*1.0 >= ($vars['syear'].$vars['smonth'])*1.0 && $vars['smonth'] != 'X' && $vars['emonth'] != 'X') {
                $sqlBig = "SELECT count(udid) as bigudid FROM (
                                SELECT udid FROM mob_client_installer 
                                WHERE `productCode`='{$productCode}' 
                                AND `addTime` > '" .$vars['syear'] . "-" . $vars['smonth']. "-1' 
                                AND `addTime` < '" .$vars['syear'] . "-" . ($vars['smonth']+1) . "-1' 
                                GROUP BY udid
                           )fff";//9月激活量
                $bigRow = $this->orderModel->getAdapter()->fetchAll($sqlBig);
                $sqlSmall = "SELECT count(num) as smalludid FROM (
                                SELECT a.udid AS num FROM mob_order AS a INNER JOIN (
                                    SELECT udid 
                                    FROM mob_client_installer 
                                    WHERE `productCode`='{$productCode}' 
                                    AND `addTime` > '" .$vars['syear'] . "-" . $vars['smonth']. "-1' 
                                    AND `addTime` < '" .$vars['syear'] . "-" . ($vars['smonth']+1) . "-1' 
                                    group by udid 
                                ) as b on a.udid=b.udid
                                WHERE `a`.`productCode`='{$productCode}' 
                                AND `addTime` > '" .$vars['eyear'] . "-" . $vars['emonth']. "-1' 
                                AND `addTime` < '" .$vars['eyear'] . "-" . ($vars['emonth']+1) . "-1' 
                                GROUP BY a.udid
                            )eee";//9月激活且在10月至少有一笔提交成功订单的用户数
                $smallRow = $this->orderModel->getAdapter()->fetchAll($sqlSmall);
                $arr = array();
                $arr['smallname'] = $vars['syear'] . '年' . $vars['smonth'].'月激活且在'.$vars['eyear'] . "年" . $vars['emonth'].'月至少有一笔提交成功订单的用户数';
                $arr['small'] = $smallRow[0]['smalludid'];
                $arr['big'] = $bigRow[0]['bigudid'];
                $arr['bigname'] = $vars['syear'] . '年' . $vars['smonth'].'月的激活量';
                $arr['pername'] = $vars['syear'] . '年' . $vars['smonth'].'月激活用户'.$vars['eyear'] . "年" . $vars['emonth'].'月订单占比';
                $arr['per'] = ($arr['big'] == 0) ? '-' : round(($arr['small']/$arr['big'])*100, 2).'%';
                $res[] = $arr;
            }
        }
        $this->view->res = $res;
    }
    
    
    
    
    
    
}